var CardContainer = function(cId,loadCard){
	var t = this;
	t.containerId = cId;
	t.containerDOM = null;
	t.cardArray = null; 
	t.height = null;
	t.width = null;
	t.loadCard = loadCard;//需要加载新页面时的回调函数
	t.init();
}
var Card = function(elem,p,pulldownText,pullupText0,pullupText1){
	var t = this;
	t.parent = p;
	t.index = p.cardArray.length;
	t.wrapperDOM = null;
	t.wrapperDOMHeight = null;
	t.scrollDOM = null;
	t.scrollDOMHeight = null;
	t.scroll = null;
	t.topDOM = null;
	t.topDOMHeight = 45;
	t.pulldownText = pulldownText;
	t.contentDOM = elem;
	t.contentDOMHeight = elem.offsetHeight;
	t.bottomDOM = null;
	t.bottomText = null;
	t.bottomDOMHeight = 45;
	t.pullupText0 = pullupText0;
	t.pullupText1 = pullupText1;
	t.pullup = null;
	t.maxSY = null;
	t.pullupReady = false;
	t.pulldownReady = false;
	t.canPullup = false;
	t.canPulldown = false;
	t.canDestory = true;
	t.init();
}
Card.prototype.init = function(){
	var t = this;
	t.wrapperDOM = document.createElement('div');
	if(!t.wrapperDOM){
		console.log('create card wrapperDOM failed');
		return;
	}
	t.wrapperDOMStyleInit();

	t.scrollDOM = document.createElement('div');
	if(!t.scrollDOM){
		console.log('create card scrollDOM failed');
		return;
	}
	t.scrollDOMStyleInit();
	t.wrapperDOM.appendChild(t.scrollDOM);

	t.topDOM = document.createElement('div');
	if(!t.topDOM){
		console.log('create topDOM failed');
		return;
	}
	t.topDOMStyleInit();
	t.scrollDOM.appendChild(t.topDOM);

	if(!t.contentDOM){
		console.log('请指定card的contentDOM');
		return;
	}
	t.scrollDOM.appendChild(t.contentDOM);

	t.bottomDOM = document.createElement('div');
	if(!t.bottomDOM){
		console.log('create bottomDOM failed');
		return;
	}
	t.bottomDOMStyleInit();
	t.scrollDOM.appendChild(t.bottomDOM);
	t.scroll = new IScroll(t.wrapperDOM,{
		probeType:2,
		scrollbars: true,
		mouseWheel: true,
		interactiveScrollbars: true,
		shrinkScrollbars: 'scale',
		fadeScrollbars: true,
		tap:true
	});
	t.scroll.minScrollY = 0;
	t.scroll.on('scroll',function(){
		if(t.canPullup){
			if (this.y < (t.maxSY-t.bottomDOMHeight+10) && !t.pullupReady) {
				t.pullup.classList.remove('giveUpRefresh');
				t.pullup.classList.add('releaseToRefresh');
				t.pullupReady = true;
				this.maxScrollY = t.maxSY-t.bottomDOMHeight;
			}
			else if (this.y > (t.maxSY-t.bottomDOMHeight+10) && t.pullupReady) {
				t.pullup.classList.remove('releaseToRefresh');
				t.pullup.classList.add('giveUpRefresh');
				t.pullupReady = false;
				this.maxScrollY = t.maxSY;
			}
		}
		if(t.canPulldown){
			if (this.y > t.topDOMHeight-10 && !t.pulldownReady) {
				t.pulldownReady = true;
				this.minScrollY = t.topDOMHeight;
			}
			else if (this.y < t.topDOMHeight-10 && t.pulldownReady) {
				t.pulldownReady = false;
				this.minScrollY = 0;
			}
		}
	});
	t.scroll.on('scrollEnd',function(){
		if(t.canPullup&&t.pullupReady){
			if(this.y != t.maxSY-t.bottomDOMHeight){
				this.scrollTo(0,t.maxSY-t.bottomDOMHeight,500)
			}
			else{
				this.maxScrollY = t.maxSY;
				t.pullup.classList.remove('releaseToRefresh');
				t.pullupReady = false;
				t.parent.showNext();
			}
		}
		if(t.canPulldown&&t.pulldownReady){
			if(this.y != t.topDOMHeight){
				this.scrollTo(0,t.topDOMHeight,500)
			}
			else{
				this.minScrollY = 0;
				t.pulldownReady = false;
				t.parent.showPrev();
			}
		}
	});
	t.scroll.on('refresh',function(){
		t.maxSY = t.scroll.maxScrollY;
		t.pullupReady = false;
		t.pulldownReady = false;
	});
	$(document).ready(function(){
		t.scroll.refresh();
		var p = t.parent;
		var len = p.cardArray.push(t);
		p.curIndex = len-1;
		if(len>=2){
           var preCard = p.cardArray[len-2];
           preCard.destory(t);
		}
	});
	t.parent.containerDOM.appendChild(t.wrapperDOM);
}
Card.prototype.enablePullup = function(){
	var t = this;
	if(!t.canPullup){
		t.canPullup = true;
		t.bottomText.innerHTML = t.pullupText1;
	}
}
Card.prototype.enablePulldown = function(){
	var t = this;
	t.canPulldown = true;
	t.scroll.minScrollY = t.topDOMHeight;
}
Card.prototype.disableDestory = function(){
	var t = this;
	t.canDestory = false;
}
Card.prototype.wrapperDOMStyleInit = function(){
	var t = this;
	t.wrapperDOM.style.position = 'relative';
	t.wrapperDOM.style.height = window.innerHeight;
	t.wrapperDOM.style.backgroundColor = '#ccc';
	t.wrapperDOM.style.overflow = 'hidden';
	t.wrapperDOM.style.zIndex = 1;
}
Card.prototype.scrollDOMStyleInit = function(){
	var t = this;
	t.scrollDOM.style.position = 'absolute';
	t.scrollDOM.style.width = '100%';
	t.scrollDOM.style.minHeight = window.innerHeight+1;
	t.scrollDOM.style.backgroundColor = '#ea4c89';
	t.scrollDOM.style.zIndex = 1;
}
Card.prototype.setBgColor = function(c){
	var t = this;
	t.scrollDOM.style.backgroundColor = c;
}
Card.prototype.topDOMStyleInit = function(){
	var t = this;
	t.topDOM.style.position = 'absolute';
	t.topDOM.style.top = -t.topDOMHeight;
	t.topDOM.style.width = '100%';
	t.topDOM.style.height = t.topDOMHeight;
	t.topDOM.style.textAlign = 'center';
	if(t.pulldownText){
		var p = document.createElement('p');
		p.innerHTML = t.pulldownText;
		t.topDOM.appendChild(p);
	}
}
Card.prototype.bottomDOMStyleInit = function(){
	var t = this;
	t.bottomDOM.style.position = 'absolute';
	t.bottomDOM.style.bottom = -t.topDOMHeight;
	t.bottomDOM.style.width = '100%';
	t.bottomDOM.style.height = t.bottomDOMHeight;
	t.bottomDOM.style.textAlign = 'center';
	if(t.pullupText1){
		var p = document.createElement('p');
		p.innerHTML = '<i class="fa fa-arrow-up"></i>';
		t.bottomText = document.createElement('span');
		t.bottomText.innerHTML = t.pullupText0;
		p.appendChild(t.bottomText);
		t.bottomDOM.appendChild(p);
		t.pullup = p.querySelector('i');
		t.pullup.style.fontSize = '20px';
		t.pullup.style.marginRight = '10px';
	}
}
Card.prototype.destory = function(nextCard){
	var t = this;
	var curPlace = 0;
	rAF(move);
	t.bottomDOM.style.opacity = 0;
	if(nextCard){
		nextCard.topDOM.style.opacity = 0;
	}
	function move(){
		if(curPlace<window.innerHeight){
			curPlace += 10+Math.abs(window.innerHeight-curPlace)/20;
			curPlace = Math.min(window.innerHeight,curPlace);
			t.wrapperDOM.style.marginTop = -curPlace+'px';
			rAF(move);
		}
		else{
			if(t.canDestory){
				t.wrapperDOM.remove();
			}
			else{
				t.bottomDOM.style.opacity = 1;
			}
			if(nextCard){
				nextCard.topDOM.style.opacity = 1;
				nextCard.scroll.refresh();
			}
		}
	}
}
CardContainer.prototype.init = function(){
	var t = this;
	t.containerDOM = document.getElementById(t.containerId);
	if(!t.containerDOM){
		console.log('指定的containerId:'+t.containerId+'不存在对应的DOM');
		return;
	}
	t.cardArray = [];
	t.height = window.innerHeight;
	t.width = Math.min(400,window.innerWidth);
	t.styleInit();
}
CardContainer.prototype.styleInit = function(){
	var t = this;
	t.containerDOM.style.position = 'relative';
	t.containerDOM.style.height = t.height;
	t.containerDOM.style.width = t.width;
	t.containerDOM.style.overflow = 'hidden';
}
CardContainer.prototype.newCard = function(elem,pulldownText,pullupText0,pullupText1){
	return new Card(elem,this,pulldownText,pullupText0,pullupText1);
}
CardContainer.prototype.getCurCard = function(){
	return this.cardArray[this.curIndex];
}
CardContainer.prototype.showNext = function(){
	var t = this;
	if(t.curIndex >= t.cardArray.length-1){
		t.loadCard(t.curIndex+1);
	}
	else{
		var curCard = t.getCurCard();
		var nextCard = t.cardArray[t.curIndex+1];
		curCard.destory(nextCard);
		t.curIndex++;
	}
}
CardContainer.prototype.showPrev = function(){
	var t = this;
	var nextCard = t.getCurCard();
	nextCard.topDOM.style.opacity = 0;
	t.curIndex--;
	var curCard = t.getCurCard();
	curCard.bottomDOM.style.opacity = 0;
	var curPlace = -window.innerHeight;
	rAF(move);
	function move(){
		if(curPlace<0){
			curPlace += 10+Math.abs(curPlace)/20;
			curPlace = Math.min(0,curPlace);
			curCard.wrapperDOM.style.marginTop = curPlace+'px';
			rAF(move);
		}
		else{
			curCard.bottomDOM.style.opacity = 1;
			nextCard.topDOM.style.opacity = 1;
			curCard.scroll.refresh();
		}
	}
}